<?php

namespace pmvc\data\bind;

use pmvc\util\StringUtil;

use Exception;

/**
 * The result of binding fields to an object.
 *
 */
class BindResult {

	private $objectErrors	= Array();
	private $fieldErrors	= Array();
	private $object			= null;

	/**
	 * @param object $object the target object
	 */
	public function __construct(&$object) {
		$this->object = $object;
	}

	/**
	 * The target object.
	 */
	public function getObject() {
		return $this->object;
	}

	/**
	 * Adds a field error.
	 * @param string $fieldName
	 * @param mixed $value
	 * @param string $message
	 * @param array $vars
	 */
	public function addFieldError($fieldName, $value, $message, $vars=Array()) {
		if (!isset($this->fieldErrors[$fieldName])) {
			$this->fieldErrors[$fieldName] = Array();
		}
		$vars = array_merge($vars, Array("fieldName" => $fieldName, "value" => $value));
		$this->fieldErrors[$fieldName] []= StringUtil::pstringArray($message, $vars);
	}

	/**
	 * Returns all field errors.
	 * @param string $fieldName
	 * @param mixed $value
	 * @param string $message
	 */
	public function getAllFieldErrors() {
		return $this->fieldErrors;
	}

	/**
	 * Returns errors for the given field.
	 * @param string $fieldName
	 * @param mixed $value
	 * @param string $message
	 */
	public function getFieldErrors($fieldName) {
		return isset($this->fieldErrors[$fieldName])
			? $this->fieldErrors[$fieldName] : Array();
	}

	/**
	 * Adds an object error.
	 * @param string $message
	 */
	public function addObjectError($message) {
		$this->objectErrors []= $message;
	}

	/**
	 * Returns all object errors.
	 * @param string $fieldName
	 * @param mixed $value
	 * @param string $message
	 */
	public function getAllObjectErrors() {
		return $this->objectErrors;
	}

}